package com.bstek.urule;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

public class ThreadUtils {

    private static Logger log=Logger.getLogger(ThreadUtils.class.getName());

    private static ThreadLocal<JSONObject> threadLocal = new ThreadLocal<>();
    private static ThreadLocal<AtomicInteger> i = new ThreadLocal<>();

    public static void init(){
        threadLocal.set(new JSONObject());
        i.set(new AtomicInteger(0));
    }


    public static JSONObject getJson(){

        JSONObject jsonObject=threadLocal.get();

        return jsonObject;
    }


    public static String get(String key){

        JSONObject jsonObject=threadLocal.get();

        return jsonObject.getString(key);
    }

    public static void set(String key,Object value){

        JSONObject jsonObject=threadLocal.get();

        if(jsonObject==null){
            init();
            jsonObject=threadLocal.get();
        }

        jsonObject.put(key+i.get().getAndIncrement(),JSON.toJSONString(value));


    }




}
